package com.aooci.ordercat.domain;

import java.io.Serializable;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.EnumType;
import javax.persistence.Enumerated;
import javax.persistence.FetchType;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToOne;
import javax.persistence.NamedQueries;
import javax.persistence.NamedQuery;
import javax.persistence.Table;

import com.aooci.ordercat.enums.OrderedItemType;

@Entity
@Table(name = "ORDERED_ITEM")
@NamedQueries({
    @NamedQuery(name="ORDERED_ITEM.selectByOrdercatId",
                query="SELECT new com.aooci.ordercat.vo.OrderedItemVo(ot.id, ot.item.label, ot.item.name, ot.quantity * ot.price, ot.item, ot.quantity, ot.price, ot.modifiedQuantity, ot.type, ot.remark, ot.sortGroup) FROM OrderedItem ot WHERE ot.ordercat.id = :ordercatId ORDER BY ot.item.itemCategory.sortGroup, ot.item.label"),
    @NamedQuery(name="ORDERED_ITEM.selectSortGroupByOrdercatId",
                query="SELECT DISTINCT(ot.sortGroup) FROM OrderedItem ot WHERE ot.ordercat.id = :ordercatId ORDER BY ot.sortGroup"),            
    @NamedQuery(name="ORDERED_ITEM.selectByOrdercatIdAndSortGroup",
                query="SELECT new com.aooci.ordercat.vo.OrderedItemVo(ot.id, ot.item.label, ot.item.name, ot.quantity * ot.price, ot.item, ot.quantity, ot.price, ot.modifiedQuantity, ot.type, ot.remark, ot.sortGroup) FROM OrderedItem ot WHERE ot.ordercat.id = :ordercatId AND ot.sortGroup = :sortGroup ORDER BY ot.item.itemCategory.sortGroup, ot.item.label"),
    @NamedQuery(name="ORDERED_ITEM.findOrderedItem",
                query="SELECT ot FROM OrderedItem ot WHERE ot.ordercat.id = :ordercatId AND ot.item.id = :itemId AND ot.remark IS NULL AND ot.type = 'NORMAL'"),
    @NamedQuery(name="ORDERED_ITEM.loadOrderedItemVo",
                query="SELECT new com.aooci.ordercat.vo.OrderedItemVo(ot.id, ot.item.label, ot.item.name, ot.quantity * ot.price, ot.item, ot.quantity, ot.price, ot.modifiedQuantity, ot.type, ot.remark, ot.sortGroup) FROM OrderedItem ot WHERE ot.ordercat.id = :ordercatId AND ot.item.id = :itemId AND ot.remark IS NULL AND ot.type = 'NORMAL'"),            
    @NamedQuery(name="ORDERED_ITEM.resetModifiedQuantityByOrdercatId",
                query="UPDATE OrderedItem ot SET ot.modifiedQuantity = 0 WHERE ot.ordercat.id = :ordercatId"),
    @NamedQuery(name="ORDERED_ITEM.getTotalOfOrderItems",
                query="SELECT SUM(ot.quantity * ot.price) FROM OrderedItem ot WHERE ot.ordercat.id = :ordercatId AND ot.type = 'NORMAL'"),
    @NamedQuery(name="ORDERED_ITEM.deleteByOrdercatId",
                query="DELETE FROM OrderedItem ot WHERE ot.ordercat.id = :ordercatId"),
    @NamedQuery(name="ORDERED_ITEM.hasKitchenItems",
                query="SELECT ot FROM OrderedItem ot WHERE  ot.ordercat.id = :ordercatId AND ot.item.printKitchen = TRUE"),
    @NamedQuery(name="ORDERED_ITEM.hasAddingKitchenItems",
                query="SELECT ot FROM OrderedItem ot WHERE  ot.ordercat.id = :ordercatId AND ot.modifiedQuantity > 0 AND ot.item.printKitchen = TRUE"),
}) 
public class OrderedItem extends IdentityEntity implements Serializable {

	private static final long serialVersionUID = 1L;

	@ManyToOne(fetch = FetchType.LAZY)
	@JoinColumn(name = "ORDERCAT_ID")
	private Ordercat ordercat;

	@ManyToOne(fetch = FetchType.EAGER)
	@JoinColumn(name = "ITEM_ID")
	private Item item;
	
	@Column(name = "QUANTITY")
	private Integer quantity;
	
	@Column(name = "MODIFIED_QUANTITY")
	private Integer modifiedQuantity;
	
	@Column(name = "PRICE")
	private float price;
	
	@Column(name = "VAT")
	private float vat;
	
	@Enumerated(EnumType.STRING)
	@Column(name = "TYPE")
	private OrderedItemType type;
	
	@Column(name = "REMARK")
	private String remark;
	
	@Column(name = "SORT_GROUP")
	private Integer sortGroup;

	/**
	 * @return the ordercat
	 */
	public Ordercat getOrdercat() {
		return ordercat;
	}

	/**
	 * @param ordercat the ordercat to set
	 */
	public void setOrdercat(Ordercat ordercat) {
		this.ordercat = ordercat;
	}

	/**
	 * @return the item
	 */
	public Item getItem() {
		return item;
	}

	/**
	 * @param item the item to set
	 */
	public void setItem(Item item) {
		this.item = item;
	}

	/**
	 * @return the vat
	 */
	public float getVat() {
		return vat;
	}

	/**
	 * @param vat the vat to set
	 */
	public void setVat(float vat) {
		this.vat = vat;
	}

	/**
	 * @return the remark
	 */
	public String getRemark() {
		return remark;
	}

	/**
	 * @param remark the remark to set
	 */
	public void setRemark(String remark) {
		this.remark = remark;
	}

	/**
	 * @return the sortGroup
	 */
	public Integer getSortGroup() {
		return sortGroup;
	}

	/**
	 * @param sortGroup the sortGroup to set
	 */
	public void setSortGroup(Integer sortGroup) {
		this.sortGroup = sortGroup;
	}

	/**
	 * @return the type
	 */
	public OrderedItemType getType() {
		return type;
	}

	/**
	 * @param type the type to set
	 */
	public void setType(OrderedItemType type) {
		this.type = type;
	}

	public Integer getQuantity() {
		return quantity;
	}

	public void setQuantity(Integer quantity) {
		this.quantity = quantity;
	}

	public Integer getModifiedQuantity() {
		return modifiedQuantity;
	}

	public void setModifiedQuantity(Integer modifiedQuantity) {
		this.modifiedQuantity = modifiedQuantity;
	}

	public float getPrice() {
		return price;
	}

	public void setPrice(float price) {
		this.price = price;
	}
	
}
